package com.zls;

import com.zls.utils.JedisPoolUtils;
import org.junit.Test;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.Tuple;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class test {
    @Test
    public void demo01(){
        // 参数一：redis服务器ip
        // 参数二：redis服务器的端口号
        Jedis jedis=new Jedis("120.27.134.168",6379);
        jedis.auth("123");
        String set = jedis.set("name", "com.zls");
        System.out.println(set);
        String name = jedis.get("name");
        System.out.println(name);
    }

    @Test
    public void StringDemo() throws InterruptedException {
        Jedis jedis =JedisPoolUtils.getResource();
        jedis.select(1);
        String name = jedis.get("name");
        System.out.println("name:"+name);
        String set = jedis.set("age", "22", "NX", "EX", 5);
        Thread.sleep(5000);
        String age = jedis.get("age");
        System.out.println("set:"+age);

        //释放资源
        JedisPoolUtils.close(jedis);
    }
    @Test
    public void HashDemo() {
        Jedis jedis =JedisPoolUtils.getResource();
        jedis.select(1);
        Map<String,String> map = new HashMap<String, String>();
        map.put("id","1002");
        map.put("name","com.zls");
        map.put("age","22");
        map.put("sex","1");
        String person = jedis.hmset("person", map);
        System.out.println(person);
        Boolean hexists = jedis.hexists("person", "name");
        System.out.println(hexists);
        Set<String> person1 = jedis.hkeys("person");
        System.out.println(person1);
        List<String> person2 = jedis.hvals("person");
        System.out.println(person2);
        //释放资源
        JedisPoolUtils.close(jedis);
    }
    @Test
    public void zsetTest() throws Exception {
        Jedis jedis = JedisPoolUtils.getResource();

        jedis.zadd("tuhaobang", 100.0, "liubei");
        jedis.zadd("tuhaobang", 15.0, "sunquan");
        jedis.zadd("tuhaobang", 80.0, "caocao");
        jedis.zadd("tuhaobang", 50.0, "guanyu");

        // 参数一：操作的zset集合
        // 参数二：最大分数
        // 参数三：最小分数
        // 参数四：第几个位置
        // 参数五：获取个数
        Set<Tuple> set = jedis.zrevrangeByScoreWithScores("tuhaobang", 999, 0, 0, 3);
        for (Tuple tuple : set) {
            String name = tuple.getElement();
            double score = tuple.getScore();
            System.out.println(name + " : " + score );
        }
        jedis.close();
    }
}
